package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.CourseType;
import cn.itsource.ymcc.mapper.CourseTypeMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.ICourseTypeService;
import cn.itsource.ymcc.vo.CourseTypeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author cff
 * @since 2024-06-11
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String COURSE_TYPE_TREE = "CourseTypeTree";

    @Override
    public JsonResult treeData() {
        if (redisTemplate.hasKey(COURSE_TYPE_TREE)) {
            Object obj = redisTemplate.opsForValue().get(COURSE_TYPE_TREE);
            return JsonResult.success().setData(obj);
        } else {
            List<CourseType> list = super.list();

            ArrayList<CourseType> top = new ArrayList<>();

            Map<Long, CourseType> courseMap = new HashMap<>(list.size());
            for (CourseType courseType : list) {
                courseMap.put(courseType.getId(), courseType);
            }
            for (CourseType courseType : list) {
                if (RConst.Num.LONG_0.equals(courseType.getPid())) {
                    top.add(courseType);
                } else {
                    CourseType pType = courseMap.get(courseType.getPid());
                    pType.getChildren().add(courseType);
                }
            }
            redisTemplate.opsForValue().set(COURSE_TYPE_TREE,top,30, TimeUnit.MINUTES);
            return JsonResult.success().setData(top);
        }
    }

    /**
     * 使用springCache做redis缓存，暂时未使用这个方法。
     * @return
     */
    @Override
    @Cacheable(cacheNames="books",key="'book600'",cacheManager = "redisCacheManager")   // books::book600
    public JsonResult springCacheTree() {

        List<CourseType> list = super.list();

        ArrayList<CourseType> top = new ArrayList<>();

        Map<Long, CourseType> courseMap = new HashMap<>(list.size());
        for (CourseType courseType : list) {
            courseMap.put(courseType.getId(), courseType);
        }
        for (CourseType courseType : list) {
            if (RConst.Num.LONG_0.equals(courseType.getPid())) {
                top.add(courseType);
            } else {
                CourseType pType = courseMap.get(courseType.getPid());
                pType.getChildren().add(courseType);
            }
        }
        return JsonResult.success().setData(top);
    }



    @Override
    public boolean save(CourseType entity) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return super.save(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return super.removeById(id);
    }

    @Override
    public boolean updateById(CourseType entity) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return super.updateById(entity);
    }

    /**
     * 面包屑接口
     * @param courseTypeId
     * @return
     */
    @Override
    public JsonResult loadCrumbs(Long courseTypeId) {
        //代码怎么写
        CourseType byId = super.getById(courseTypeId);//当前的数据
        //有几层
        String[] split = byId.getPath().split("\\.");
        List<CourseTypeVo> list = new ArrayList<>(split.length);
        for (String s : split) {
            //自己
            CourseTypeVo courseTypeVo = new CourseTypeVo();
            CourseType courseType = super.getById(Long.valueOf(s));
            courseTypeVo.setCourseType(courseType);
            //兄弟
            Long pid = courseType.getPid();
            courseTypeVo.setCourseTypes(super.list(
                    new LambdaQueryWrapper<CourseType>().eq(CourseType::getPid, pid)));
            list.add(courseTypeVo);
        }
        //返回是一个list
        return JsonResult.success().setData(list);

    }
}
